//
//  XWXSqliteQueryCommand+Table.m
//  XWXSqlite
//
//  Created by 许卫星 on 17/9/4.
//  Copyright © 2017年 wxxu. All rights reserved.
//

#import "XWXSqliteQueryCommand+Table.h"
#import "XWXSqliteQueryCommand+Query.h"
#import "NSString+XWXSqliteSQL.h"
#import "XWXSqliteConfiguration.h"
#import "XWXSqliteModelTool.h"

@implementation XWXSqliteQueryCommand (Table)

- (XWXSqliteQueryCommand *)createTable:(NSString *)tableName columnInfo:(NSString *)columnInfo primaryKey:(NSString *)primaryKey
{
    [self resetQueryCommand];
    NSString *safeTableName = [tableName safeSQLMetaString];
    if (kXWXSqlite_isEmptyString(safeTableName)) {
        return self;
    }
    
    [self.sqlString appendFormat:@"CREATE TABLE IF NOT EXISTS %@ (%@,primary key(%@));", safeTableName, columnInfo, primaryKey];
    
    return self;
}

- (XWXSqliteQueryCommand *)dropTable:(NSString *)tableName
{
    [self resetQueryCommand];
    if (kXWXSqlite_isEmptyString(tableName)) {
        return self;
    }
    NSString *safeTableName = [tableName safeSQLMetaString];
    [self.sqlString appendFormat:@"DROP TABLE IF EXISTS %@;", safeTableName];
    return self;
}

- (XWXSqliteQueryCommand *)alterTable:(NSString *)tableName retableName:(NSString *)retableName
{
    [self resetQueryCommand];
    if (kXWXSqlite_isEmptyString(tableName)) {
        return self;
    }
    NSString *safeTableName = [tableName safeSQLMetaString];
    NSString *safereTableName = [retableName safeSQLMetaString];
    [self.sqlString appendFormat:@"ALTER TABLE %@ RENAME TO %@;", safeTableName,safereTableName];
    return self;
}

- (XWXSqliteQueryCommand *)addColumn:(NSString *)columnName columnInfo:(NSString *)columnInfo tableName:(NSString *)tableName
{
    [self resetQueryCommand];
    NSString *safeColumnName = [columnName safeSQLMetaString];
    NSString *safeColumnInfo = [columnInfo safeSQLMetaString];
    NSString *safeTableName = [tableName safeSQLMetaString];
    
    if (kXWXSqlite_isEmptyString(safeTableName) || kXWXSqlite_isEmptyString(safeColumnInfo) || kXWXSqlite_isEmptyString(safeColumnName)) {
        return self;
    }
    
    [self.sqlString appendFormat:@"ALTER TABLE %@ ADD COLUMN %@ %@;", safeTableName, safeColumnName, safeColumnInfo];
    
    return self;
}

- (XWXSqliteQueryCommand *)createIndex:(NSString *)indexName tableName:(NSString *)tableName indexedColumnList:(NSArray *)indexedColumnList whereCondition:(NSString *)condition isUnique:(BOOL)isUnique
{
    [self resetQueryCommand];
    NSString *safeIndexName = [indexName safeSQLMetaString];
    NSString *safeTableName = [tableName safeSQLMetaString];
    if (kXWXSqlite_isEmptyString(safeTableName) || kXWXSqlite_isEmptyString(safeIndexName) || indexedColumnList == nil) {
        return self;
    }
    
    NSString *indexedColumnListString = [indexedColumnList componentsJoinedByString:@","];
    
    if (isUnique) {
        [self.sqlString appendFormat:@"CREATE UNIQUE INDEX IF NOT EXISTS "];
    } else {
        [self.sqlString appendFormat:@"CREATE INDEX IF NOT EXISTS "];
    }
    
    [self.sqlString appendFormat:@"%@ ON %@ (%@) ", safeIndexName, safeTableName, indexedColumnListString];
    
    return [self where:condition];
}

- (XWXSqliteQueryCommand *)dropIndex:(NSString *)indexName
{
    [self resetQueryCommand];
    NSString *safeIndexName = [indexName safeSQLMetaString];
    if (kXWXSqlite_isEmptyString(safeIndexName)) {
        return self;
    }
    [self.sqlString appendFormat:@"DROP INDEX IF EXISTS %@;", safeIndexName];
    return self;
}

- (XWXSqliteQueryCommand *)isTableExists: (NSString *)tableName
{
    [self resetQueryCommand];
    NSString *safeIndexName = [tableName safeSQLMetaString];
    if (kXWXSqlite_isEmptyString(safeIndexName)) {
        return self;
    }
    
    [self.sqlString appendFormat:@"select name from sqlite_master"];
    return self;
}

- (XWXSqliteQueryCommand *)tableAllColumnNames :(NSString *)tableName
{
    [self resetQueryCommand];
    NSString *safeIndexName = [tableName safeSQLMetaString];
    if (kXWXSqlite_isEmptyString(safeIndexName)) {
        return self;
    }
    [self.sqlString appendFormat:@"select * from sqlite_master where type = 'table' and name = '%@'", tableName];
    
    return self;
}
@end
